package com.ug_project.views

import android.app.Activity

import android.view.View
import android.widget.*
import android.widget.Button
import android.widget.CheckedTextView
import android.widget.LinearLayout
import android.widget.RadioButton
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager.widget.ViewPager


var<T:View> T.click: (view: T) -> Unit
    get() = {Exception("بۇ خاسلىقنى ئىلىشنى قوللىمايدۇ")}
    set(value) {
        val action:(View)->Unit ={
            value(it as T)
        }
        this.setOnClickListener(action)
    }

fun <T : View> T.click(clickAction: (view: T) -> Unit){
    this.click = clickAction
}

var<T:View> T.longClick: (view: T) -> Boolean
    get() = {Exception("بۇ خاسلىقنى ئىلىشنى قوللىمايدۇ");false}
    set(value) {
        val action:(View)->Boolean ={
            value(it as T)
        }
        this.setOnLongClickListener(action)
    }

fun <T : View> T.longClick(clickAction: (view: T) -> Boolean){
    this.longClick = clickAction
}


var TextView.isEmpty: Boolean
get() = this.text.toString().trim().isNullOrEmpty()
set(value) {}

var TextView.content: String
get() = this.text.toString()
set(value) {this.text = value}

fun View.findView(Resource_ID: Int): View = this.findViewById(Resource_ID)
fun Activity.findView(Resource_ID: Int): View = this.findViewById(Resource_ID)
fun <T> View.findView(id: Int): T = findView(id) as T
fun <T> Activity.findView(id: Int): T = findView(id) as T
fun View.findButton(Resource_ID: Int) : android.widget.Button = findView<Button>(Resource_ID)
fun View.findTextView(Resource_ID: Int) : android.widget.TextView = findView<TextView>(Resource_ID)
fun View.findImageView(Resource_ID: Int) : android.widget.ImageView = findView<ImageView>(Resource_ID)
fun View.findEditText(Resource_ID: Int) : android.widget.EditText = findView<android.widget.EditText>(Resource_ID)
fun View.findCheckBox(Resource_ID: Int) : android.widget.CheckBox = findView<android.widget.CheckBox>(Resource_ID)
fun View.findCheckedTextView(Resource_ID: Int) : CheckedTextView = findView<android.widget.CheckedTextView>(Resource_ID)
fun View.findRadioButton(Resource_ID: Int) : RadioButton = findView<android.widget.RadioButton>(Resource_ID)
fun View.findListView(Resource_ID: Int) : ListView = findView<android.widget.ListView>(Resource_ID)
fun View.findRecyclerView(Resource_ID: Int) : RecyclerView = findView<RecyclerView>(Resource_ID)
fun View.findLinearLayout(Resource_ID: Int) : LinearLayout = findView<LinearLayout>(Resource_ID)
fun View.findRelativeLayout(Resource_ID: Int) : RelativeLayout = findView<RelativeLayout>(Resource_ID)
fun View.findViewPager(Resource_ID: Int) : ViewPager = findView<ViewPager>(Resource_ID)

val View.meaSuredHeight: Int
    get() {
        val msp = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)
        this.measure(msp, msp)
        return this.measuredHeight
    }

val View.meaSuredWidth: Int
    get(){
        val msp = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)
        this.measure(msp, msp)
        return this.measuredWidth
    }